package testCases;

import common.InitAndClose;
import org.junit.jupiter.api.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.concurrent.TimeUnit;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FriendListTest extends InitAndClose {

    @Order(1)
    @Test
    public void friendDisplayTest() {
        login();
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        // 添加 wangwu 和 zhaoliu
        webDriver.findElement(By.cssSelector("#inputBox")).sendKeys("wangwu", Keys.ENTER);
        webDriver.findElement(By.cssSelector("#add-friend > li > button")).click();
        webDriver.findElement(By.cssSelector("#addFriendBtn")).click();
        webDriver.findElement(By.cssSelector("#inputBox")).sendKeys(Keys.CONTROL, "a");
        webDriver.findElement(By.cssSelector("#inputBox")).sendKeys("zhaoliu", Keys.ENTER);
        webDriver.findElement(By.cssSelector("#add-friend > li > button")).click();
        webDriver.findElement(By.cssSelector("#addFriendBtn")).click();

        // 同意好友申请
        WebDriver webDriver2 = new ChromeDriver();
        webDriver2.get("http://8.130.172.144:8081/login.html");
        webDriver2.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        webDriver2.findElement(By.cssSelector("#username")).sendKeys("wangwu");
        webDriver2.findElement(By.cssSelector("#password")).sendKeys("123456", Keys.ENTER);
        webDriver2.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[3]/div[4]")).click();
        webDriver2.findElement(By.cssSelector("#friend-req > li > div > div > button.accept-btn")).click();

        webDriver2.get("http://8.130.172.144:8081/login.html");
        webDriver2.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        webDriver2.findElement(By.cssSelector("#username")).sendKeys("zhaoliu");
        webDriver2.findElement(By.cssSelector("#password")).sendKeys("123456", Keys.ENTER);
        webDriver2.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[3]/div[4]")).click();
        webDriver2.findElement(By.cssSelector("#friend-req > li > div > div > button.accept-btn")).click();
        webDriver2.quit();

        // 查看刚添加完的好友是否会立即出现在好友列表中
        // 在查看好友列表, 是否有 wangwu 和 zhaoliu
        try {
            WebDriverWait wait = new WebDriverWait(webDriver, 3);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#friend-list")));
        } catch (Exception e) {
            webDriver.navigate().refresh();
        }

        // 获取好友列表 ul 下的 所有 li
        List<WebElement> liList = webDriver
                .findElement(By.cssSelector("#friend-list"))
                .findElements(By.tagName("li"));

        // 遍历 li
        int res = 0;
        for (WebElement li : liList) {
            WebElement h4 = li.findElement(By.tagName("h4"));
            String name = ((JavascriptExecutor) webDriver).executeScript(
                    "return arguments[0].innerText;", h4
            ).toString();
            System.out.println(name);

            // 查看 wangwu 和 zhaoliu 是否都出现在好友列表中
            if (name.equals("wangwu") || name.equals("zhaoliu")) {
                res++;
            }
        }

        if (res != 2) {
            Assertions.fail("王五或赵六不在好友列表");
        }
        System.out.println("测试通过");
    }

    @Order(2)
    @Test
    public void clickTest() {
        login();
        // 点击好友列表的各个好友
        WebElement ul = webDriver.findElement(By.xpath("//*[@id=\"friend-list\"]"));
        List<WebElement> liList = ul.findElements(By.tagName("li"));

        for (WebElement li : liList) {
            // 查看右边聊天界面的名字是否与当前的点击的名字相同
            String clickName =
                    ((JavascriptExecutor) webDriver).executeScript(
                            "return arguments[0].innerText;", li.findElement(By.tagName("h4"))
                    ).toString();

            // 点击好友
            ((JavascriptExecutor) webDriver).executeScript(
                    "arguments[0].click();", li
            );

            String sessionName =
                    webDriver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[1]")).getText();

            System.out.println("clickName: " + clickName + " sessionName: " + sessionName);
            Assertions.assertEquals(clickName, sessionName);
        }

        System.out.println("测试通过");
    }
}
